package org.dizena.base.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.dizena.modules.master.bean.User;
import org.dizena.modules.master.dao.UserDao;

import javax.annotation.Resource;

//@Component
public class TokenUserUtil
{
    @Resource
    private UserDao dao;

    public User current()
    {
        Subject subject = SecurityUtils.getSubject();
        String account = subject.getPrincipal().toString();
        User user = dao.queryAccount(account);
        if (user == null)
        {
            throw new RuntimeException("Token not be use for user");
        }
        return user;
    }

    public Boolean checkRole(String role)
    {
        Subject subject = SecurityUtils.getSubject();
        String account = subject.getPrincipal().toString();
        User user = dao.queryAccount(account);
        if (user == null)
        {
            throw new RuntimeException("Token not be use for user");
        }
        return user.getRoles().contains(role);
    }


}
